<?php
namespace frontend\controllers;

use common\models\Region;
use common\models\Subscription;
use common\services\BannerServices;
use common\services\ExhibitionCategoryServices;
use common\services\ExhibitionHallServices;
use common\services\ExhibitionServices;
use common\services\NewsServices;
use common\services\RegionServices;
use frontend\base\BaseFrontController;
use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;

/**
 * Site controller
 */
class HomeController extends BaseFrontController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    //'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        //Banner
        $bannerService = new BannerServices();
        $banner = $bannerService->getPageBanner('index');

        //展会列表
        $exhibitionService = new ExhibitionServices();
        //$params = array(
        //    'ajax' => 1,
        //    'limit' => 6
        //);
        //$exhibition = $exhibitionService->searchPage($params);

        //推荐展会
        $recommendParams = array(
            'recommend' => 1,
            'ajax' => 1,
            'limit' => 12
        );
        $recommend = $exhibitionService->searchPage($recommendParams);
        //近期展会
        $nearParams = array(
            'ajax' => 1,
            'limit' => 12,
            'where' => array('>=', 'starttime', time()),
            'order' => array('order' => SORT_DESC, 'id' => SORT_ASC),
        );
        $near = $exhibitionService->searchPage($nearParams);

        //行业新闻
        $newService = new NewsServices();
        $newParams = array(
            'ajax' => 1,
            'limit' => 5,
            'categoryId' => 3
        );
        $news = $newService->searchPage($newParams);
        //热门城市
        $regionService = new RegionServices();
        $params = array(
            'status' => Region::STATUS_FLAG_NORMAL,
            'is_array' => 1,
            'field' => array(
                'id',
                'name'
            ),
            'is_hot' => 1,
            'defaultPageSize' => 90,
        );
        $region = $regionService->search($params);
        if ($region->getCount() > 0) {
            $regionData = $region->getModels();
        }else{
            $regionData = array();
        }
        //推荐城市
        $params = array(
            'status' => Region::STATUS_FLAG_NORMAL,
            'is_array' => 1,
            'field' => array(
                'id',
                'name'
            ),
            'is_index' => 1,
            'defaultPageSize' => 27,
        );
        $recommendCity = $regionService->search($params);
        if ($recommendCity->getCount() > 0) {
            $recommendCity = $recommendCity->getModels();
        }else{
            $recommendCity = array();
        }

        //行业分类
        $exhibitionCategoryService = new ExhibitionCategoryServices();
        $exhibitionCategory = $exhibitionCategoryService->getIndexCategory();
        //行业展会推荐分类
        $params = array(
            'is_index' => 1,
            'limit' => 3
        );
        $recommendCategory = $exhibitionCategoryService->getIndexCategory($params);

        return $this->render('index', [
            'banner' => $banner,
            //'exhibition' => $exhibition['data'],
            'news' => $news['data'],
            'recommend' => $recommend['data'],
            'near' => $near['data'],
            'category' => $exhibitionCategory,
            'recommendCategory' => $recommendCategory,
            'regionHot' => $regionData,
            'regionRecommend' => $recommendCity,
        ]);
    }

    /**
     * 订阅
     */
    public function actionSubscribe()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $data = Yii::$app->request->post();

        $data['uid'] = Yii::$app->user->identity->getId();
        $datas['_csrf'] = $data['_csrf'];
        unset($data['_csrf']);
        $datas['Subscription'] = $data;
        $model = new Subscription();
        $model->load($datas);
        $rst = $model->save();
        if($rst){
            return ['code' => 0, 'data' => $datas, 'msg' => '订阅成功'];
        }else{
            return ['code' => 1, 'data' => $datas, 'msg' => '订阅失败'];
        }

    }

    /**
     * 获取地区下的展馆
     * @param $region_id
     * @return string
     */
    public function actionGetHall(){
        $region_id = Yii::$app->request->get('region_id');
        $exhibitionHallService = new ExhibitionHallServices();
        $data = $exhibitionHallService->getRegionHall($region_id, 9);

        return $this->renderAjax('hall', [
            'region_id' => $region_id,
            'data' => $data,
        ]);
    }

    /**
     * 获取展会分类数据
     * @return string
     */
    public function actionGetList() {
        $categoryId = Yii::$app->request->get('categoryId');
        $exhibitionService = new ExhibitionServices();
        $params = array(
            'ajax' => 1,
            'limit' => 6,
            'category_id' => $categoryId,
        );
        $exhibition = $exhibitionService->searchPage($params);

        return $this->renderAjax('list', [
            'category_id' => $categoryId,
            'data' => $exhibition['data'],
        ]);
    }
}
